from src.enum.exception import ExceptionEnum
from src.enum.task import ExecResultEnum, CrawlerStatusEnum
from starlette import status

# api接口响应异常
class ResponseException(Exception):
    def __init__(self, status_code: int,error: ExceptionEnum):
        self.status_code = status_code
        self.error_code = error.code
        self.message = error.msg

# 应用异常
class ApplicationException(ResponseException):
    def __init__(self, error: ExceptionEnum):
        super().__init__(status_code = status.HTTP_200_OK, error = error)

# 爬虫异常
class CrawlerException(Exception):
    def __init__(self,crawler_status: CrawlerStatusEnum, exec_result: ExecResultEnum,error: ExceptionEnum):
        self.crawler_status = crawler_status
        self.exec_result = exec_result
        self.reason = "[" + error.code + "]" + error.msg

# 爬虫完成异常
class CrawlerFinishException(CrawlerException):
    def __init__(self,exec_result: ExecResultEnum, error: ExceptionEnum):
        self.crawler_status = CrawlerStatusEnum.FINISH
        self.exec_result = exec_result
        self.reason = "[" + error.code + "]" + error.msg

# 爬虫延期执行异常
class CrawlerDelayException(CrawlerException):
    def __init__(self,exec_result: ExecResultEnum, error: ExceptionEnum):
        self.crawler_status = CrawlerStatusEnum.ABNORMAL_DELAY
        self.exec_result = exec_result
        self.reason = "[" + error.code + "]" + error.msg

# 爬虫失败异常
class CrawlerFailureException(CrawlerException):
    def __init__(self,exec_result: ExecResultEnum, error: ExceptionEnum):
        self.crawler_status = CrawlerStatusEnum.FAILURE
        self.exec_result = exec_result
        self.reason = "[" + error.code + "]" + error.msg

class WaybillNumberInvalidException(CrawlerFailureException):
    def __init__(self):
        super().__init__(exec_result= ExecResultEnum.WAYBILL_FORMAT_INVALID, error= ExceptionEnum.BILL_OF_LADING_CHECKED_FAILED)

# 查无数据异常
class NonDataException(CrawlerFinishException):
    def __init__(self):
        super().__init__(exec_result=ExecResultEnum.NON_DATA,error=ExceptionEnum.NO_DATA_FOUND)

# 无插件异常
class NonPluginException(CrawlerFailureException):
    def __init__(self):
        super().__init__(exec_result=ExecResultEnum.INIT,error=ExceptionEnum.NO_COMPANY)

# 机器人校验失败异常
class RebotVerificationFailureException(CrawlerFailureException):
    def __init__(self):
        super().__init__(exec_result=ExecResultEnum.ROBOT_VERIFICATION_FAILURE,
                         error=ExceptionEnum.VERIFICATION_CODE_ERROR)

# 验证码错误异常
class VerificationCodeErrorException(CrawlerDelayException):
    def __init__(self):
        super().__init__(exec_result=ExecResultEnum.ROBOT_VERIFICATION_FAILURE,
                         error=ExceptionEnum.VERIFICATION_CODE_ERROR)

# 网站加载错误异常
class WebpageLoadingErrorException(CrawlerDelayException):
    def __init__(self):
        super().__init__(exec_result=ExecResultEnum.NULL_DATA,
                         error=ExceptionEnum.WEB_PAGE_LOAD_FAILED)